cap log close
clear
program drop _all

set more off
set mem 1000m

#delimit;

log using "${d1}log/tasp_earnings_paco.log", replace;

forvalues tsp_y = 1992/2000 {;

 local tsp_ya = `tsp_y'-1800;

   forvalues tsp_fs = 1/4 {;

   disp "TSP Year=`tsp_ya' Semester=`tsp_fs'";
***
* Program to generate quarterly earnings for students entering college up to 28 quarters
* For later cohorts, positive earnings available only for earlier time frame
***;

use if tspyr==`tsp_ya' & firstsem==`tsp_fs' using "${d1}data/tasp192_200_withall.dta", clear;
sort altpid;

* initialize quarterly earnings outcomes to zero;
foreach var in earn_1 earn_2 earn_3 earn_4 earn_5 earn_6 earn_7 earn_8 earn_9 earn_10 earn_11 earn_12 earn_13 earn_14 earn_15 earn_16 earn_17 earn_18 earn_19 earn_20 earn_21 earn_22 earn_23 earn_24 earn_25 earn_26 earn_27 earn_28 {;
	gen `var'=0;
};


 ** Note: define quarter based on last month of their first term:
	 fall sem, Q4
	 spring sem, Q2
	 both summer semesters, Q3;

        if `tsp_fs'==1 {;  
	 local fq=4;
	 local fy=`tsp_y'-1;
	};
	else if `tsp_fs'==2 {; 
	 local fq=2;
	 local fy=`tsp_y';
	};
	else if (`tsp_fs'==3 | `tsp_fs'==4) {;
	 local fq=3;
	 local fy=`tsp_y';
	};
forvalues e_quarter = 1/28 {;

 if mod(`e_quarter',4)<=(5-`fq') {;
  local twc_q=mod(`e_quarter',4)+(`fq'-1);
 };
 else if mod(`e_quarter',4)>(5-`fq')  {;
   local twc_q=mod(`e_quarter',4)-(5-`fq');
 };
 local twc_y=int((`fq'+`e_quarter'-2)/4)+`fy';

	di "     *** total quarters = `e_quarter' *** ";
	display "     TWC File **** uniquetwc`twc_y'_`twc_q' ***" ;
 
	* 2004 4th quarter earnings and later not available;
	if ~((`twc_y'==2004 & `twc_q'==4) | (`twc_y'>2004)) {;

		qui merge altpid using "${d1}data/newtwc/uniquetwc`twc_y'_`twc_q'", uniqusing nokeep keep(sumwages);
		qui replace earn_`e_quarter'=sumwages if _merge==3;
		label var earn_`e_quarter' "`e_quarter' Quarter Earnings";
		sort altpid;
								}; /* close if statement */
		cap drop sumwages _merge;
		
			};	/* closes equarter loop */

save "${d1}tmp/taspearn`tsp_y'_`tsp_fs'", replace;
 
 };	/* closes tsp_fs loop */

}; /* closing final loop */



# delimit cr;

/* **************************************************************************
Now generate analyis variables and stack the datasets 
************************************************************************** */

forvalues y=1992/1999 {
 local tspy=`y'-1800
 forvalues fs=1/4 {
 
 if `y'<1999 | `fs'==1 {

 *Merge enrollment information
 use "${d1}tmp/taspearn`y'_`fs'", clear
 bysort altpid school tspyr firstsem semester: assert _N==1
 cap drop srtotcredit_* 
 cap drop jrtotcredit_* 
 cap drop jrvalidmatch_r1* 
 cap drop srvalidmatch_r1*
 qui merge altpid school tspyr firstsem semester using ${d1}data/tasp_withde_outc_`tspy', /* 
  */ unique sort nokeep keep(srtotcredit_* jrtotcredit_* jrvalidmatch_r1* srvalidmatch_r1*)
 assert _merge==3
 drop _merge
 sort altpid 
 
 *Note: mrgalloutcomes.do only gets data for the next 6 acad yrs. which is enough to get 6 yrs worth of data.
 *To get 7 complete years, we need to merge on the 7th acad yr after starting for students starting in semester>1

 if `fs'>1 {
  local endyr=`tspy'+7
  local addsemneeded=`fs'-1
  if `fs'==2 {
   local addsemneeded=2
  }
  forvalues sem=1/`addsemneeded' {

  gen jrtotcredit_7_`sem'=0
  gen srtotcredit_7_`sem'=0


  ******* Junior colleges ******
  cap confirm file ${d1}data/highered/report1jc`endyr'_`sem'.dta
  if _rc==0 {
   sort altpid
   *Merge dataset and identify matched records
   qui merge altpid using ${d1}data/highered/report1jc`endyr'_`sem'.dta, ///
   uniqusing nokeep keep(altpid totcredit)

   qui replace jrtotcredit_7_`sem'=totcredit if _merge==3 & totcredit!=.

   drop _merge totcredit 
  }
  ******* Senior colleges ******
  cap confirm file ${d1}data/highered/report1sr`endyr'_`sem'.dta
  if _rc==0 {
   sort altpid
   *Merge dataset and identify matched records
   qui merge altpid using ${d1}data/highered/report1sr`endyr'_`sem'.dta, ///
    uniqusing nokeep keep(altpid totcredit)

   qui replace srtotcredit_7_`sem'=totcredit if _merge==3 & totcredit!=.

   drop _merge totcredit 
   }
   
   }   /* close semester loop */
  }    /* close not first semester condition */


 *Create enrollment info for year 5, 6, 7 (note year "0" on variable names refers to tspyr of first semester)
 *Year "1" on TWC var names refers to year of first semester.) 
 *   (note that some yr6 validmatch=0 but that's because out of the 6yr window, NOT because no merge!)
 
 
 forvalues yr=5/7 {
  local sdy=`yr'-1
  local yp1=`yr'
  gen byte enrolled`yr'=0
  gen byte ftenrolled`yr'=0
  label var enrolled`yr' "Enrolled in TWC yr `y'"
  label var ftenrolled`yr' "Full time enrolled in TWC yr `yr'"
  if `fs'==1 {
   qui replace enrolled`yr'=1 if ((jrtotcredit_`sdy'_1>=3 | srtotcredit_`sdy'_1>=3)+(jrtotcredit_`sdy'_2>=3 | srtotcredit_`sdy'_2>=3)+ /*
    */ (jrtotcredit_`sdy'_3>=3 | srtotcredit_`sdy'_3>=3)+(jrtotcredit_`sdy'_4>=3))>0
   qui replace ftenrolled`yr'=1 if ((jrtotcredit_`sdy'_1>=9 | srtotcredit_`sdy'_1>=9)+(jrtotcredit_`sdy'_2>=9 | srtotcredit_`sdy'_2>=9)+ /*
    */ (jrtotcredit_`sdy'_3>=9 | srtotcredit_`sdy'_3>=9)+(jrtotcredit_`sdy'_4>=9))>0
  }
  else if `fs'==2 {
   qui replace enrolled`yr'=1 if ((jrtotcredit_`sdy'_2>=3 | srtotcredit_`sdy'_2>=3)+(jrtotcredit_`sdy'_3>=3 | srtotcredit_`sdy'_3>=3)+ /*
   */ (jrtotcredit_`sdy'_4>=3) + (jrtotcredit_`yp1'_1>=3 | srtotcredit_`yp1'_1>=3)+ (jrtotcredit_`yp1'_2>=3 | srtotcredit_`yp1'_2>=3))>0 
   qui replace ftenrolled`yr'=1 if ((jrtotcredit_`sdy'_2>=9 | srtotcredit_`sdy'_2>=9)+(jrtotcredit_`sdy'_3>=9 | srtotcredit_`sdy'_3>=9)+ /*
   */ (jrtotcredit_`sdy'_4>=9) + (jrtotcredit_`yp1'_1>=9 | srtotcredit_`yp1'_1>=9)+ (jrtotcredit_`yp1'_2>=9 | srtotcredit_`yp1'_2>=9))>0 
  }
  else if `fs'==3 | `fs'==4 {
   qui replace enrolled`yr'=1 if ((jrtotcredit_`sdy'_3>=3 | srtotcredit_`sdy'_3>=3)+(jrtotcredit_`sdy'_4>=3) /*
   */  + (jrtotcredit_`yp1'_1>=3 | srtotcredit_`yp1'_1>=3)+ (jrtotcredit_`yp1'_2>=3 | srtotcredit_`yp1'_2>=3))>0 
   qui replace ftenrolled`yr'=1 if ((jrtotcredit_`sdy'_3>=9 | srtotcredit_`sdy'_3>=9)+(jrtotcredit_`sdy'_4>=9) /*
   */  + (jrtotcredit_`yp1'_1>=9 | srtotcredit_`yp1'_1>=9)+ (jrtotcredit_`yp1'_2>=9 | srtotcredit_`yp1'_2>=9))>0 
  }
 }

 gen earn_yr5=earn_17+earn_18+earn_19+earn_20
 gen earn_yr6=earn_21+earn_22+earn_23+earn_24
 gen earn_yr7=earn_25+earn_26+earn_27+earn_28

 drop jrtotcredit_*_* srtotcredit_*_* jrvalidmatch_r1* srvalidmatch_r1* oldjr* num earn_1-earn_8
 qui compress

 save ${d1}tmp/tempearn`tspy'_`fs', replace
 } /* if earlier than 199(1) */

 }
}

use ${d1}tmp/tempearn199_1, clear

forvalues tspy=192/198 {
 forvalues fs=1/4 {
 cap confirm file "${d1}tmp/tempearn`tspy'_`fs'.dta"
 if _rc==0 {
  qui append using ${d1}tmp/tempearn`tspy'_`fs'
 }
}
}

save ${d1}data/taspearn_allyears, replace


forvalues tspy=192/198 {
 forvalues fs=1/4 {
 cap rm "${d1}tmp/tempearn`tspy'_`fs'.dta"
}
}
